﻿using System.Collections.Generic;
using System.Linq;

namespace Domain
{
    public class Converter
    {
        private Dictionary<string, double> mQuotations;

        public Converter()
        {
            mQuotations = new Dictionary<string, double>
            {
                {"ruble", 1.0d}, 
                {"dollar", 31.49d}, 
                {"euro", 40.22d}, 
                {"yena", 0.39d}, 
                {"frank", 33.34d}
             };
        }

        public Dictionary<string, string> GetQuotations()
        {
            return mQuotations.ToDictionary(pair => pair.Key, pair => pair.Value.ToString());
        }

        public void UpdateQuotations(Dictionary<string, double> quotations)
        {
            if (quotations.Any(pair => pair.Value <= 0))
            {
                throw new NegativeCountException();
            }

            mQuotations = new Dictionary<string, double>(quotations);
        }

        public void AddCurrency(string name, double value)
        {
            if (mQuotations.ContainsKey(name)) throw new AlreadyContainException();
            if (value <= 0) throw new NegativeCountException();
            mQuotations.Add(name, value);
        }

        public int GetCurrencyListLength()
        {
            return mQuotations.Count;
        }

        public double GetCurrencyPrice(string from)
        {
            if (mQuotations.ContainsKey(from))
            {
                return mQuotations[from];
            }
            throw new NotContainException();
        }

        public double ConvertCurrency(string from, string to, double fromCount)
        {
            if (fromCount < 0)
            {
                throw new NegativeCountException();
            }
            
            if (!mQuotations.ContainsKey(from))
            {
                throw new NotContainException();
            }

            if (!mQuotations.ContainsKey(to))
            {
                throw new NotContainException();
            }

            var res = fromCount * mQuotations[@from] / mQuotations[to];

            return res;
        }
    }
}
